package com.emapp.lccode.activity

import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import com.amap.api.location.AMapLocationClient
import com.emapp.lccode.R
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseApplication
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.databinding.ActivityLogoBinding
import com.emapp.lccode.mode.LoginKtViewModel
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.isInternetConnected
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.showMessage
import com.emapp.lccode.utils.toStartActivity
import com.emapp.lccode.view.PrivacyDialog
import com.emapp.lccode.view.PrivacyDialog.ConfirmListener
import com.gyf.immersionbar.ktx.immersionBar
import com.mylhyl.circledialog.CircleDialog
import me.hgj.jetpackmvvm.base.appContext
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class LogoKtActivity : BaseActivity1<LoginKtViewModel, ActivityLogoBinding>(),
    ConfirmListener {
    val privacyDialog: PrivacyDialog by lazy { PrivacyDialog() }
    var animationEnd = false
    var apiGet = false
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            transparentBar()
        }
        //  mViewModel.getDistrict("0")
        init()
    }

    override fun createObserver() {
        mViewModel.run {
            amapKey.observe(this@LogoKtActivity) { result ->
                if (result != null) {
                    apikey = result.amap_key
                    log_e("apikey--" + result.amap_key)
                }
                if (animationEnd) {
                    if (MmkvUtil.getBoolean(BaseConfig.CANCEL_PRIVACY_SHOW, false)) {
                        setKey(apikey)
                        checkLogin()
                    } else {
                        privacyDialog.show(supportFragmentManager, "")
                    }
                }
                apiGet = true
            }
        }
        if (!isInternetConnected(appContext)) {
            CircleDialog.Builder()
                .setTitle("手机无网络")
                .setCancelable(false)
                .setSubTitle("请连接互联网后访问App")
                // .setNegative("取消", null)
                .setPositive("确定") {
                    //Toaster.show("正在重启")
                    true
                }
                .show(supportFragmentManager)
        } else {
            mViewModel.getAmapKey()
        }

    }

    private fun init() {
        val animation = AnimationUtils.loadAnimation(this, R.anim.logo)
        animation.setAnimationListener(StartAnimationListener())
        mViewBind.imageview.startAnimation(animation)
    }

    private fun isAutoLogin(): Boolean {
        return MmkvUtil.getBoolean("isAutoLogin", false)
    }

    private fun checkLogin() {
        if (isAutoLogin()) {
            toMain()
        } else {
            BaseApplication.instance.setUser(null)
            toLogin()
        }
    }

    private fun toMain() {
        log_e("main")
        toStartActivity(MainKtActivity::class.java)
        finish()
    }

    private fun toLogin() {
        log_e("login")
        toStartActivity(LoginKtActivity::class.java)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }

    override fun onConfirm() {
        //AMapLocationClient.setApiKey("f4c4a0de8b19bb09b16c50b3d6caf1f0")
        setKey(apikey)
        checkLogin()
    }

    override fun onCancel() {
        showMessage("请同意并接受《用户协议》、《隐私政策》全部条款后再开始使用我们的服务。", "温馨提示", "确定", positiveAction = {
            privacyDialog.show(supportFragmentManager, "")
        })
    }

    var apikey = "4ce2bf0cda030e636cdfd7f9609ece9e"

    inner class StartAnimationListener : Animation.AnimationListener {
        override fun onAnimationStart(animation: Animation) {}
        override fun onAnimationEnd(animation: Animation) {
            if (apiGet) {
                if (MmkvUtil.getBoolean(BaseConfig.CANCEL_PRIVACY_SHOW, false)) {
                    setKey(apikey)
                    checkLogin()
                } else {
                    privacyDialog.show(supportFragmentManager, "")
                }
            }
        }

        override fun onAnimationRepeat(animation: Animation) {}
    }

    fun setKey(key: String) {
        AMapLocationClient.setApiKey(key)
        AMapLocationClient.updatePrivacyShow(this@LogoKtActivity, true, true)
        AMapLocationClient.updatePrivacyAgree(this@LogoKtActivity, true)
    }
}
